const app = getApp()
import {urls} from '../../utils/urls'
let _this;
Page({
  data: {
    task_list:[],
    page:1,
    load:false,
    size:5,
    tag:['校园帮','论坛'],
    flag:0,
    url:'get',
    user_id:wx.getStorageSync("user").id,
    role_list:wx.getStorageSync('user').role,
    category_list: [
      { text: '全部任务', value: 999},
      { text: '快递代取', value: 1},
      { text: '校内跑腿', value: 3 },
    ],
    order_list: [
      { text: '默认排序', value: '-id' },
      { text: '价格排序', value: '-price' },
    ],
    category: 999,
    order: '-id',
    count : wx.getStorageSync('task').count,
    is_courier : false
  },
  onLoad: function () {
    _this = this
    if(wx.getStorageSync('category')){
      this.setData({
        category:wx.getStorageSync('category')
      })
    }
    _this.getList(0)
  },
  getList(type){
    if (type == 0) {
      this.data.page = 1
    } else {
      this.data.page += 1
    }
    this.setData({
      load:true
    })
    let params ={
      ordering:this.data.order,
      category:this.data.category,
      page: this.data.page,
      size: this.data.size
    }
    if(this.data.category == 999){
      delete params["category"]
    }
    app.http('GET',urls.task ,params, 
    function (res) {
      wx.setStorageSync('task', res.results)
      wx.stopPullDownRefresh()
      if (res.code == 100) {
        let new_task = res.results.results
        for (let i in new_task) {
          new_task[i].create_time = app.formatMsgTime(new_task[i].create_time)
        }
        let task = []
        if (type == 0) {
          for (let i in new_task) {
            if(new_task[i].task_status==0 | new_task[i].task_status==4 ){}
            else{
              task.push(new_task[i])
            }
          }
        } else {
          task = _this.data.task_list
          for (let i in new_task) {
            if(new_task[i].task_status==0 | new_task[i].task_status==4 ){}
            else{
              task.push(new_task[i])
            }           
          }
        }
        _this.setData({
          task_list: task,
          count: res.results.count,
          load:false
        })
      } else {
        _this.setData({
          load: false
        })
        wx.showToast({
          title: '到底了！',
          icon: 'none'
        })
      }
    })
  },
  onShow: function () {
    if(wx.getStorageSync('category')){
      // console.log(222);
      _this.setData({
        category:wx.getStorageSync('category')
      })
    }
    _this.getList(0)
  },
  onPullDownRefresh: function () {
    _this.getList(0)
  },
  onReachBottom: function () {
    if(this.data.task_list.length < this.data.count){
      _this.getList(1)
    }
  },
  changeTag(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      flag: index
    })
    if(index == 0){
      _this.data.url = 'get'
      _this.data.wheres = 'a_id=' + wx.getStorageSync("area").pk_id
      _this.data.sorts = ""
      _this.data.fields = ''
      _this.getList(0)
    }else if(index == 1){
      _this.data.url = 'get2'
      _this.data.fields = 'helplist.*,wxuser.phone,wxuser.dphone,wxuser.avatar_url,wxuser.nick_name'
      _this.data.wheres = 'helplist.is_delete=0 and user_id=' + wx.getStorageSync("user").id
      _this.data.sorts = "helplist.create_time desc"
      _this.getList(0)
    }else{
      _this.data.url = 'get2'
      _this.data.fields = 'helplist.*,wxuser.phone,wxuser.dphone,wxuser.avatar_url,wxuser.nick_name'
      _this.data.wheres = 'helplist.is_delete=0 and jd_id=' + wx.getStorageSync("user").id
      _this.data.sorts = "helplist.create_time desc"
      _this.getList(0)
      _this.getWxsmData()
    }
  },
  takeOrder(e){
    let index = e.currentTarget.dataset.index
    let task_detail = _this.data.task_list[index]
	for(let i in _this.data.role_list){
		_this.data.role_list[i] == 3
    _this.setData({
      is_courier : true
    })
	}
  if (_this.data.is_courier){
    _this.takeDo(task_detail)
  }else{
    wx.showModal({
      title: '提示',
      content: '您还不是接单员,是否前往申请？',
      showCancel:true ,
      confirmText: '立即前往',
      complete: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/mine/apply/apply',
          })
        }
      }
    })
  } 
  },
  changeCategory(value){
    // console.log(value.detail);
    _this.setData({
      category: value.detail
    })
    _this.getList(0)
  },
  changeOrder(value){
    // console.log(value.detail);
    _this.setData({
      order : value.detail
    })
    _this.getList(0)
  },
  takeDo(detail){
    let update_dict ={
      id : detail.id,
      task_status : 2,
      consumer_id : wx.getStorageSync('user').id
    } 
    console.log(update_dict);
    app.http('PUT',urls.task, update_dict,
    function(res){
      if(res.code == 100){
        wx.showToast({
          title: '接单成功！',
        })
      }
    })
    _this.getList(0)
  }
})